import java.io.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author anan
 * @create 2022-03-24 16:57
 */
public final class MyFileReader {

    public static void readAll(MyFile myFile, String url){
        readDemand(myFile, url + "demand.csv");
        readSiteBandwidth(myFile, url + "site_bandwidth.csv");
        readQos(myFile, url + "qos.csv");
        readQosConstraint(myFile, url + "config.ini");
    }

    public static void readDemand(MyFile myFile, String url){
        ArrayList<String[]> demands = readCsv(url);

        //设置demands里面的clientId
        List<String> clientId = new ArrayList<>();
        String[] clientIdString = demands.get(0);
        for(int i = 1; i < clientIdString.length; i++){
            clientId.add(clientIdString[i]);
        }
        myFile.setClientId(clientId);

        //设置demands里面的demand
        List<Map<String,Integer>> demand = new ArrayList<>();
        for(int i = 1; i < demands.size(); i++){
            Map<String, Integer> map = new HashMap<>();
            for(int j = 1; j < clientIdString.length; j++){
                map.put(clientIdString[j], Integer.parseInt(demands.get(i)[j]));
            }
            demand.add(map);
        }
        myFile.setDemand(demand);
    }

    public static void readSiteBandwidth(MyFile myFile, String url){
        ArrayList<String[]> list = readCsv(url);
        List<String> edgeNodeId = new ArrayList<>();
        Map<String, Integer> siteBandwidth = new HashMap<>();
        for(int i = 1; i < list.size(); i++){
            edgeNodeId.add(list.get(i)[0]);
            siteBandwidth.put(list.get(i)[0], Integer.parseInt(list.get(i)[1]));
        }
        myFile.setEdgeNodeId(edgeNodeId);
        myFile.setSiteBandwidth(siteBandwidth);

    }

    public static void readQos(MyFile myFile, String url){
        ArrayList<String[]> list = readCsv(url);
        Map<String, Integer> qos = new HashMap<>();
        for(int i = 1; i < list.size(); i++){
            for(int j = 1; j < list.get(i).length; j++){
                String clientId = list.get(0)[j];
                String edgeId = list.get(i)[0];
                qos.put(clientId + edgeId, Integer.parseInt(list.get(i)[j]));
            }
        }
        myFile.setQos(qos);
    }

    public static void readQosConstraint(MyFile myFile, String url){
        ArrayList<String[]> list = readCsv(url);
        String s = list.get(1)[0];
        String[] split = s.split("=");
        myFile.setQosConStraint(Integer.parseInt(split[1]));
    }



    public static ArrayList<String[]> readCsv(String filepath) {
        File csv = new File(filepath); // CSV文件路径
        csv.setReadable(true);//设置可读
        csv.setWritable(true);//设置可写
        BufferedReader br = null;
        try {
            br = new BufferedReader(new FileReader(csv));
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        String line = "";
        String[] everyLine;
        ArrayList<String[]> allString = new ArrayList<>();
        try {
            while ((line = br.readLine()) != null) // 读取到的内容给line变量
            {

                everyLine = line.split(",");
                allString.add(everyLine);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return allString;
    }
}
